import { mapState, mapMutations } from 'vuex';

const { platform } = uni.getSystemInfoSync()

const tv = {
	data () {
		return {
			wifiName: '', // 当前wifi名称
			currentTvVolume: 0,
			setScreenClicked: false
		}
	},
	computed: {
		...mapState({
			devices: state => state.devices,
			isPushing: state => state.isPushing,
			playUrl: state => state.playUrl,
			searchStatus: state => state.searchStatus,
			pushStatus: state => state.pushStatus
		})
	},
	methods: {
		...mapMutations(['setIsPushingStatus', 'setSearchStatus', 'setCurrentTvVolume']),
		getCurrentWifiName () {
			uni.$emit('getCurrentWifiName',(name) => {
				console.log('当前wifi名称:', name);
				this.wifiName = name;
			})
			// try {
			// 	Projection.getConnectWifiName((name) => {
			// 		console.log('当前wifi名称:', name);
			// 		this.setCurrentWifiName(name);
			// 	})
			// } catch (e) {
			// 	console.log(e);
			// }
		},
		refreshDevices () {
			uni.$emit('refreshDevices');
			this.searchStatus = true;
		},
		setScreen (item) {
			if (this.setScreenClicked) return;
			this.setScreenClicked = true;
			try {
				if ( platform === 'ios' && this.playUrl.includes('.pcm')) {
					uni.showToast({
						title: '请使用airplay播放',
						icon: 'none'
					})
					return;
				}
				let params;
				if (platform === 'ios') {
					params = {
						playUrl: this.playUrl,
						device: item
					}	
				} else {
					params = Object.assign(item, {
						playUrl: this.playUrl
					})
				}
				console.log('set Screen params', params);
				uni.$emit('startPush', params);
				uni.$once('onProjectionPlay', () => {
					// 投屏成功后
					uni.showToast({
						title: '投屏成功',
						icon: 'none'
					});
					setTimeout(()=> {
						uni.navigateBack({
							success: () => {
								this.setIsPushingStatus(true);
							}
						});
					}, 1500);
				})
				setTimeout(() => {
					this.setScreenClicked = false
				}, 3000)
			} catch (e) {
				console.log(e)
			}
		},
		stopPush () {
			this.setIsPushingStatus(false);
			uni.$emit('stopPush');
		},
		pausePush () {
			uni.$emit('pausePush',() => {
				this.isPlaying = false;
			})
		},
		resumePush () {
			this.isPlaying = true;
			uni.$emit('resumePush');
		},
		tvVolumePlus () {
			this.currentTvVolume++;
			this.setTvVolume(this.currentTvVolume);
		},
		tvVolumeReduce () {
			if (this.currentTvVolume === 0) return;
			this.currentTvVolume--;
			this.setTvVolume(this.currentTvVolume);
		},
		setTvVolume (value) {
			try {
				let volume;
				if (platform === 'android') {
					volume = {
						volume: parseInt(value * 2)
					}
				} else {
					volume = value * 2;
				}
				console.log('setTvVolume params', volume);
				uni.$emit('setVolume', volume);
				// this.setCurrentTvVolume(volume);
				console.log('currentTvVolum',this.currentTvVolume);
			} catch (e) {
				console.error(e);
			}
		},
		tvSeek (value) {
			try {
				let position;
				if (platform === 'android') {
					position = {
						position: parseInt(value * 1000)
					}
				} else {
					position = value
				}
				console.log('seek params:', position)
				uni.$emit('seek', position);
			} catch (e) {
				console.error(e);
			}
		},
	}
}

export default tv